package org.ccnetconfig.ccdroid;

import java.util.HashMap;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingsAdapter extends BaseExpandableListAdapter {
	private static final String										TAG				= "SettingsAdapter";

	private Context																context		= null;
	private LinkedList<Settings>									settings	= null;
	private HashMap<Integer, LinkedList<Project>>	projects	= null;

	/**
	 * 
	 * @param context
	 * @param settings
	 */
	public SettingsAdapter ( Context context, LinkedList<Settings> settings ) {
		this.setContext ( context );
		this.setSettings ( settings );
		this.setProjects ( new HashMap<Integer, LinkedList<Project>> () );

		DataProvider dp = new DataProvider ( this.getContext () );
		for ( int i = 0; i < this.getSettings ().size (); ++i ) {
			try {
				Settings s = this.getSettings ().get ( i );
				LinkedList<Project> projects = dp.getProjects ( null, s.getId () );
				if ( projects.isEmpty () ) {
					Log.d ( SettingsAdapter.TAG, "Adding sample project" );
					Project p = new Project ( "ccnetconfig", s );
					p.setActivity ( Project.UNKNOWN );
					p.setCategory ( "" );
					p.setLastLabel ( "0.6.123.456" );
					p.setLastStatus ( Project.SUCCESS );
					p.setEnabled ( true );
					p.setStage ( "" );
					dp.insertProject ( null, p );
					projects.add ( p );
				}
				this.getProjects ().put ( i, projects );
			} catch ( Exception ex ) {
				Log.e ( SettingsAdapter.TAG, ex.getMessage () );
			}
		}
	}

	/**
	 * @param context
	 *          the context to set
	 */
	public void setContext ( Context context ) {
		this.context = context;
	}

	/**
	 * @return the context
	 */
	public Context getContext ( ) {
		return context;
	}

	/**
	 * @param settings
	 *          the settings to set
	 */
	private void setSettings ( LinkedList<Settings> settings ) {
		this.settings = settings;
	}

	/**
	 * @return the settings
	 */
	public LinkedList<Settings> getSettings ( ) {
		return settings;
	}

	/**
	 * @param projects
	 *          the projects to set
	 */
	private void setProjects ( HashMap<Integer, LinkedList<Project>> projects ) {
		this.projects = projects;
	}

	/**
	 * @return the projects
	 */
	public HashMap<Integer, LinkedList<Project>> getProjects ( ) {
		return projects;
	}

	public Project getChild ( int groupPosition, int childPosition ) {
		return this.getProjects ().get ( groupPosition ).get ( childPosition );
	}

	public long getChildId ( int groupPosition, int childPosition ) {
		return this.getChild ( groupPosition, childPosition ).getId ();
	}

	public TextView getGenericView ( ) {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams ( ViewGroup.LayoutParams.FILL_PARENT, 64 );

		TextView textView = new TextView ( this.getContext () );
		textView.setLayoutParams ( lp );
		// Center the text vertically
		textView.setGravity ( Gravity.CENTER_VERTICAL | Gravity.LEFT );
		// Set the text starting position
		textView.setPadding ( 36, 0, 0, 0 );
		return textView;
	}

	public View getChildView ( int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent ) {

		LinearLayout ll = new LinearLayout ( this.getContext () );
		ll.setOrientation ( LinearLayout.HORIZONTAL );
		ImageView status = new ImageView ( this.getContext () );
		status.setMaxHeight ( 16 );
		status.setMaxWidth ( 16 );
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams ( ViewGroup.LayoutParams.FILL_PARENT, 64 );
		status.setLayoutParams ( lp );

		Project p = this.getChild ( groupPosition, childPosition );

		if ( p.getLastStatus ().equals ( Project.FAILURE ) || p.getLastStatus ().equals ( Project.EXCEPTION ) ) {
			status.setImageBitmap ( BitmapFactory.decodeResource ( parent.getResources (), R.drawable.error ) );
		} else if ( p.getLastStatus ().equals ( Project.SUCCESS ) ) {
			status.setImageBitmap ( BitmapFactory.decodeResource ( parent.getResources (), R.drawable.check ) );
		} else {
			status.setImageBitmap ( BitmapFactory.decodeResource ( parent.getResources (), R.drawable.unknown ) );
		}

		TextView textView = getGenericView ();
		textView.setText ( getChild ( groupPosition, childPosition ).getName () );

		ll.addView ( status );
		ll.addView ( textView );
		return ll;

	}

	public int getChildrenCount ( int groupPosition ) {
		return this.getProjects ().get ( groupPosition ).size ();
	}

	public Settings getGroup ( int groupPosition ) {
		return this.getSettings ().get ( groupPosition );
	}

	public int getGroupCount ( ) {
		return this.getSettings ().size ();
	}

	/**
	 *
	 */
	public long getGroupId ( int groupPosition ) {
		return this.getGroup ( groupPosition ).getId ();
	}

	public View getGroupView ( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent ) {
		TextView textView = getGenericView ();
		textView.setText ( getGroup ( groupPosition ).getHost () + " (" + this.getChildrenCount ( groupPosition ) + ")" );
		return textView;

	}

	public boolean hasStableIds ( ) {
		return true;
	}

	public boolean isChildSelectable ( int groupPosition, int childPosition ) {
		return true;
	}

}
